using System.Data;
using System.Linq.Expressions;
using System.Text.RegularExpressions;

namespace 第六次作业
{
    public partial class Form1 : Form
    {
        private string expression = "";
        private double result = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void numButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            expression += button.Text;
            textBox1.Text = expression;
        }

        private void operationBttn_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (expression == "")
            {
                MessageBox.Show("请输入数字");
                return;
            }
            expression += button.Text;
            textBox1.Text = expression;
        }

        private void equBttn_Click(object sender, EventArgs e)
        {
            if (expression == "")
            {
                MessageBox.Show("请输入表达式");
                return;
            }
            Regex regex = new Regex(@"^[\d+\-*/(). ]+$");
            if (!regex.IsMatch(expression))
            {
                MessageBox.Show("表达式格式有误");
                expression = "";
                return;
            }
            try
            {
                result = Convert.ToDouble(new DataTable().Compute(expression, null));
                textBox1.Text = expression + "=" + result.ToString();
                expression = "";
            }
            catch
            {
                MessageBox.Show("表达式有误");
            }
        }

        private void acBttn_Click(object sender, EventArgs e)
        {
            expression = "";
            result = 0;
            textBox1.Text = "";
        }
    }
}